package cn.iocoder.yudao.module.mqtt.service.devicedriver;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.mqtt.controller.admin.devicedriver.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicedriver.DeviceDriverDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.mqtt.dal.mysql.devicedriver.DeviceDriverMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.mqtt.enums.ErrorCodeConstants.*;

/**
 * 设备驱动 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DeviceDriverServiceImpl implements DeviceDriverService {

    @Resource
    private DeviceDriverMapper deviceDriverMapper;

    @Override
    public Long createDeviceDriver(DeviceDriverSaveReqVO createReqVO) {
        // 插入
        DeviceDriverDO deviceDriver = BeanUtils.toBean(createReqVO, DeviceDriverDO.class);
        deviceDriverMapper.insert(deviceDriver);
        // 返回
        return deviceDriver.getId();
    }

    @Override
    public void updateDeviceDriver(DeviceDriverSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceDriverExists(updateReqVO.getId());
        // 更新
        DeviceDriverDO updateObj = BeanUtils.toBean(updateReqVO, DeviceDriverDO.class);
        deviceDriverMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceDriver(Long id) {
        // 校验存在
        validateDeviceDriverExists(id);
        // 删除
        deviceDriverMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceDriverListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceDriverExists(ids);
        // 删除
        deviceDriverMapper.deleteByIds(ids);
        }

    private void validateDeviceDriverExists(List<Long> ids) {
        List<DeviceDriverDO> list = deviceDriverMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_DRIVER_NOT_EXISTS);
        }
    }

    private void validateDeviceDriverExists(Long id) {
        if (deviceDriverMapper.selectById(id) == null) {
            throw exception(DEVICE_DRIVER_NOT_EXISTS);
        }
    }

    @Override
    public DeviceDriverDO getDeviceDriver(Long id) {
        return deviceDriverMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceDriverDO> getDeviceDriverPage(DeviceDriverPageReqVO pageReqVO) {
        return deviceDriverMapper.selectPage(pageReqVO);
    }

}